package kong_gui_items;

import java.util.LinkedList;

import barrel_kong.GameState;


public class Barrel extends BaseGraphics {

	//If moving is vertical or horizontal
	Boolean vertical;
	Boolean arrowsShown;
	//Total pixel deplacement in both liberty direction for barrel 
	int libertyLatitude = 120;
	// Current state of the slider
	int currentSensorActivation;
	int minx;
	int miny;
	int maxx;
	int maxy;

	
	private void setBoundaries(boolean vertical, int x, int y){

		if(this instanceof FinishPlatform)
		{
			libertyLatitude = 0;
		}

		if(vertical)
		{
			minx = x;
			maxx = x;
			miny = y-libertyLatitude;
			maxy = y+libertyLatitude;
		}	
		else
		{
			minx = x-libertyLatitude;
			maxx = x+libertyLatitude;
			miny = y;
			maxy = y;
		}
		if(minx < 0){minx=0;}
		if(miny < 0){miny=0;}
		if(maxx > GameState.windowWidth){maxx=GameState.windowWidth;}
		if(maxy > GameState.windowHeight){maxy=GameState.windowHeight;}
	}

	public Boolean isVertical() { return vertical; }
	
	
	// used by Finish platform
	public Barrel(int x, int y, String imageName, Boolean rotate, Boolean vertical) {
		super(x, y, imageName, vertical);
		this.vertical = vertical;
		setBoundaries(vertical, x, y);
	}

	public Barrel(int x, int y, Boolean rotate, boolean vertical) {
		super(x, y, ".\\Resources\\Picts\\barrel.png", rotate);
		this.vertical = vertical;
		setBoundaries(vertical, x, y);

	}

	public Barrel(int x, int y, String imageName, Boolean rotate, Boolean vertical, boolean arrowsShown) {
		super(x, y, imageName, vertical);
		this.vertical = vertical;
		setBoundaries(vertical, x, y);
		this.arrowsShown= arrowsShown;
	}







	public Boolean getArrowsShown() {
		return arrowsShown;
	}

	public void setPosition(Integer positionValue, LinkedList<BaseGraphics> list){


		currentSensorActivation = positionValue;

		Integer initialX = x;
		Integer initialY = y;
		
		if(vertical)
		{
			setY((int)(miny+(maxy-miny)*(positionValue/1000.0)));
		}	
		else
		{
			setX((int)(minx+(maxx-minx)*(positionValue/1000.0)));
		}	
		
		//Check if the barrel hit the rocks when it's moves manually
		if (list != null)
		{
			for(BaseGraphics forme : list){
				if(forme instanceof Wide_Rock || forme instanceof L_Rock ||
				   forme instanceof High_Rocks || forme instanceof HudLife || 
				   forme instanceof DisplayBananaCount)
				{
					if( forme.intersectOnBorders(this))
					{
						setX(initialX);
						setY(initialY);
						break;
					}
				}
			}
		}
		
		if(x+width > GameState.windowWidth)
			x = GameState.windowWidth-width;
		if(y+height > GameState.windowHeight)
			y = GameState.windowHeight-height;
		
		if(x < 0)
			x = 0;
		if(y < 0)
			y = 0;
		
	}

	public int getCurrentSensorActivation() {
		return currentSensorActivation;
	}


	public void setCurrentSensorActivation(int currentSensorActivation) {
		this.currentSensorActivation = currentSensorActivation;
		//there rock hit check is not necessary. it's an automatic move to be synchronized
		//with the cursor position
		setPosition(currentSensorActivation, null);

	}

	private static final long serialVersionUID = 1L;


}
